﻿using LibNiconico.NiconicoApi.Extentions;
using LibNiconico.NiconicoApi.Internal;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace LibNiconico.NiconicoApi.Lives.Broadcast
{

    /// <summary>
    /// ステータス
    /// </summary>
    public enum NicoLiveBroadcastWaitInfoStreamStatus
    {
        /// <summary>
        /// 設定なし
        /// </summary>
        None,

        /// <summary>
        /// 順番待ち中
        /// </summary>
        Waiting,

        /// <summary>
        /// 不明
        /// </summary>
        Unknown,

        /// <summary>
        /// 開始可能
        /// </summary>
        StartPossible,

        /// <summary>
        /// 未定義の値
        /// </summary>
        Undefined,
    }


    /// <summary>
    /// ステータス2
    /// </summary>
    public enum NicoLiveBroadcastWaitInfoCurrentStatus
    {
        /// <summary>
        /// 設定なし
        /// </summary>
        None,

        /// <summary>
        /// 放送中
        /// </summary>
        Onair,

        /// <summary>
        /// 枠が終了している
        /// </summary>
        Closed,

        /// <summary>
        /// 未定義の値
        /// </summary>
        Undefined,

    }

    /// <summary>
    /// ニコ生枠待ち情報取得レスポンス
    /// </summary>
    public class NicoLiveBroadcastWaitInfoResponse : NicoApiInfoXmlResponseBase
    {
        private NicoLiveBroadcastWaitInfoResponse(XDocument document):base(document)
        {

        }

        /// <summary>
        /// 順番待ち数
        /// </summary>
        public int Count { get; private set; }

        /// <summary>
        /// 割り込みが発生したか判定
        /// </summary>
        public bool ChargeFlag { get; private set; }


        /// <summary>
        /// ステータス
        /// </summary>
        public NicoLiveBroadcastWaitInfoStreamStatus StreamStatus { get; private set; }


        /// <summary>
        /// 割り込み発生数
        /// </summary>
        public int ChargeCount { get; private set; }

        /// <summary>
        /// 開始予定日時
        /// </summary>
        public DateTime StartTime { get; private set; }

        /// <summary>
        /// 枠取得後の入室待機時間
        /// </summary>
        public long RemainTime { get; private set; }


        /// <summary>
        /// ステータス2
        /// </summary>
        public NicoLiveBroadcastWaitInfoCurrentStatus CurrentStatus { get; private set; }


        internal static NicoLiveBroadcastWaitInfoResponse Parse(XDocument document)
        {

            var liveBroadcast = new NicoLiveBroadcastWaitInfoResponse(document);

            var waitInfoNodes = document.Descendants("waitinfo");

            if (waitInfoNodes.Count() > 1)
            {

                var waitInfoNode = waitInfoNodes.First();
                liveBroadcast.Count = waitInfoNode.GetElementAsInt("count");
                liveBroadcast.ChargeFlag = waitInfoNode.GetElementAsBool("charge_flag");
                liveBroadcast.ChargeCount = waitInfoNode.GetElementAsInt("charge_count");
                liveBroadcast.StartTime = waitInfoNode.GetElementAsDateTime("start_time");

                var streamStatus = waitInfoNode.GetElementAsString("stream_status");

                switch(streamStatus)
                {
                    case "0":
                        liveBroadcast.StreamStatus = NicoLiveBroadcastWaitInfoStreamStatus.Waiting;
                        break;
                        
                    case "1":
                        liveBroadcast.StreamStatus = NicoLiveBroadcastWaitInfoStreamStatus.Unknown;
                        break;

                    case "2":
                        liveBroadcast.StreamStatus = NicoLiveBroadcastWaitInfoStreamStatus.StartPossible;
                        break;

                    default:
                        liveBroadcast.StreamStatus = NicoLiveBroadcastWaitInfoStreamStatus.Undefined;
                        break;

                }


                liveBroadcast.RemainTime = waitInfoNode.GetElementAsLong("remain_time");

                var currentStatus = waitInfoNode.GetElementAsString("currentstatus");

                switch (currentStatus)
                {
                    case "onair":

                        liveBroadcast.CurrentStatus = NicoLiveBroadcastWaitInfoCurrentStatus.Onair;
                        break;

                    case "closed":
                        liveBroadcast.CurrentStatus = NicoLiveBroadcastWaitInfoCurrentStatus.Closed;
                        break;

                    default:
                        liveBroadcast.CurrentStatus = NicoLiveBroadcastWaitInfoCurrentStatus.Undefined;
                        break;
                }
            }





            return liveBroadcast;
        }

        
    }
}
